<template>
	<div class="main-login">
		<div class="container">
			<div class="left-con"></div>
			<div class="login">
				<div class="logo-wrapper">
					<img src="@/assets/logo.png" />
					<!-- <img src="@/assets/logo_1.png" /> -->
					<span>{{ sysConfig.sysTitle }}</span>
				</div>
				<el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
					<el-form-item prop="username">
						<el-input v-model="loginForm.account" type="text" auto-complete="off" placeholder="请输入用户名">
							<!-- <span slot="prefix" class="input-label">账号</span> -->
							<i slot="prefix" class="el-icon-user input-label"></i>
						</el-input>
					</el-form-item>
					<el-form-item prop="password">
						<el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="请输入密码" @keyup.enter.native="handleLogin">
							<!-- <span slot="prefix" class="input-label">密码</span> -->
							<i slot="prefix" class="el-icon-lock input-label"></i>
						</el-input>
					</el-form-item>
					<el-form-item prop="rememberMe" style="text-align: right;">
						<el-checkbox v-model="loginForm.rememberMe">记住密码 </el-checkbox>
					</el-form-item>
					<el-form-item style="width:100%;">
						<el-button :loading="loading" size="medium" type="primary" style="width:100%;" @click.native.prevent="handleLogin">
							<span v-if="!loading">登录</span>
							<span v-else>登 录 中...</span>
						</el-button>
					</el-form-item>
				</el-form>
			</div>
		</div>
	</div>
</template>

<script>
	import Cookies from 'js-cookie'
	import { encrypt } from '@/utils/jsencrypt'

	export default {
		name: 'Login',
		data() {
			return {
				codeUrl: '',
				cookiePassword: '',
				loginForm: {
					account: '',
					password: '',
					sourceType: 'web',
					rememberMe: false,
				},
				loginRules: {
					account: [
						{
							required: true,
							trigger: 'blur',
							message: '用户名不能为空',
						},
					],
					password: [
						{
							required: true,
							trigger: 'blur',
							message: '密码不能为空',
						},
					],
				},
				loading: false,
				redirect: undefined,
			}
		},
		watch: {
			$route: {
				handler: function(route) {
					this.redirect = route.query && route.query.redirect
				},
				immediate: true,
			},
		},
		created() {
			this.getCookie()
			var _self = this
			document.onkeydown = function(e) {
				var key = window.event.keyCode
				if (key == 13) {
					_self.handleLogin()
				}
			}
		},
		methods: {
			getCookie() {
				const account = Cookies.get('account')
				const password = Cookies.get('password')
				const rememberMe = Cookies.get('rememberMe')
				this.loginForm = {
					account: account === undefined ? this.loginForm.account : account,
					password: password === undefined ? this.loginForm.password : password,
					rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
				}
			},
			handleLogin() {
				this.$refs.loginForm.validate(valid => {
					if (valid) {
						this.loading = true
						if (this.loginForm.rememberMe) {
							Cookies.set('account', this.loginForm.account, {
								expires: 30,
							})
							Cookies.set('password', this.loginForm.password, {
								expires: 30,
							})
							Cookies.set('rememberMe', this.loginForm.rememberMe, {
								expires: 30,
							})
						} else {
							Cookies.remove('account')
							Cookies.remove('password')
							Cookies.remove('rememberMe')
						}
						let loginParam = {}
						this.$set(loginParam, 'account', encrypt(this.loginForm.account))
						this.$set(loginParam, 'password', encrypt(this.loginForm.password))
						this.$set(loginParam, 'sourceType', 'web')
						this.$store
							.dispatch('Login', loginParam)
							.then(res => {
								if (res.success) {
									this.$store.dispatch('clearAllTags')
									this.$router.push('/')
									document.onkeydown = null
								} else {
									this.message(res.msg, 3)
									this.loading = false
								}
							})
							.catch(() => {
								this.loading = false
							})
					}
				})
			},
		},
	}
</script>

<style rel="stylesheet/scss" lang="scss">
	.main-login {
		background-image: url('../assets/image/backNew.jpg');
		height: 100%;
		background-size: 100% 100%;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.container {
		width: 60%;
		height: 50%;
		background-color: #fff;
		border-radius: 10px;
		box-shadow: 0px 0px 12px 0px rgba(223, 237, 234, 1);
		display: flex;
		min-height: 360px;
	}

	.left-con {
		height: 100%;
		width: 50%;
		background-image: url('~@/assets/image/login-left.jpg');
		background-size: 100% 100%;
		border-top-left-radius: 10px;
		border-bottom-left-radius: 10px;
	}

	.login {
		display: flex;
		justify-content: center;
		height: 100%;
		width: 50%;
		min-width: 360px;
		flex-direction: column;
		border-top-right-radius: 10px;
		border-bottom-right-radius: 10px;
	}

	.login {
		padding: 0 40px;
	}

	.logo-wrapper {
		font-size: 26px;
		color: RGBA(0, 135, 133, 1);
		font-weight: bold;
		display: flex;
		align-items: center;
		margin-bottom: 30px;
		justify-content: center;

		img {
			margin-right: 10px;
			height: 51px;
			width: 51px;
		}
	}

	.title {
		margin: 0px auto 30px auto;
		text-align: center;
		color: #707070;
	}

	.login-form {
		.login-head {
			height: 56px;
			margin-bottom: 56px;
			width: 380px;

			.login-head-div {
				/*width: 296px;
				display: flex;*/
				height: 100%;

				.logo {
					color: rgba(255, 255, 255, 1);
					font-size: 65px;
					font-family: 'iconfont';
				}

				.sys-icon {
					margin-left: 42px;
					width: 64px;
					height: 56px;

					img {
						width: 64px;
						height: 56px;
					}
				}

				.sys-title {
					margin-left: 16px;
					width: 216px;
					height: 56px;
					color: rgba(255, 255, 255, 1);

					.sys-title-zhn {
						width: 240px;
						height: 40px;
						font-size: 36px;
						margin-top: -5px;
						letter-spacing: 24px;
					}

					.sys-title-en {
						margin-top: 6px;
						width: 216px;
						height: 16px;
						font-size: 9px;
						letter-spacing: 3px;
					}
				}
			}
		}

		.el-form-item {
			margin-bottom: 15px;
		}

		.el-input {
			input {
				height: 57px;
				// background: rgba(0, 0, 0, 0.4);
				border-radius: 5px;
				border: 1px solid rgba(180, 180, 180, 1);
				padding-left: 55px;
				font-size: 20px;
			}

			// ::v-deep.el-input__icon {
			//   font-size: 22px;
			// }

			.input-label {
				margin-left: 10px;
				font-size: 30px;
				margin-top: 12px;
				font-family: MicrosoftYaHei;
				color: rgba(180, 180, 180, 1);
				display: inline-block;
			}
		}

		.el-button--medium {
			height: 57px;
			// box-shadow: 0px 10px 12px 0px rgba(0, 128, 255, 0.4);
			border-radius: 5px;
			font-size: 16px;
			font-family: MicrosoftYaHei;
		}
	}

	.login-tip {
		font-size: 13px;
		text-align: center;
		color: #bfbfbf;
	}

	.login-code {
		width: 33%;
		height: 38px;
		float: right;

		img {
			cursor: pointer;
			vertical-align: middle;
		}
	}

	.el-login-footer {
		height: 40px;
		line-height: 40px;
		position: fixed;
		bottom: 0;
		width: 100%;
		text-align: center;
		color: #fff;
		font-family: Arial;
		font-size: 12px;
		letter-spacing: 1px;
	}
</style>
